package com.example.learn.service;

import com.example.learn.domain.IncomeChart;
import com.example.learn.domain.Optionaler;
import com.example.learn.entity.Account;
import com.example.learn.entity.Income;
import com.example.learn.entity.User;
import com.example.learn.mapper.AccountMapper;
import com.example.learn.mapper.IncomeMapper;
import com.example.learn.utile.TimeUtil;
import com.example.learn.utile.charutile;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;

@Service
public class IncomeService {
    @Resource
    IncomeMapper incomeMapper;
    @Resource
    AccountMapper accountMapper;

    public List<Income> getincome(String queryTime, String queryname, User user) {
        Set<String> names = new HashSet<String>();
        if (queryname.isEmpty()) {
            queryname = "family";
        }
        if (queryname.equals("family")) {
            //先找出家庭人员
            List<Account> accounts = accountMapper.getFamilyName(user.getName());

            for (Account account : accounts
            ) {
                names.add(account.getName());
                names.add(account.getAccount());
            }
        } else {
            names.add(queryname);
        }
        String queryTimeStart = TimeUtil.getfirstDay(queryTime);
        String queryTimeEnd = TimeUtil.getlastDay(queryTime);
        List<Income> incomeList = incomeMapper.getByName(names, queryTimeStart, queryTimeEnd, queryname);
        return incomeList;

    }

    public String addincome(Income incomeList, User user) {
        if (StringUtils.isEmpty(incomeList.getId())) {
            incomeList.setId(UUID.randomUUID().toString());
            incomeList.setName(user.getName());
            incomeList.setDisable(true);
            incomeList.setDate(TimeUtil.getCurrentTime());
            int i = incomeMapper.insert(incomeList);
        } else {
            incomeList.setName(user.getName());
            incomeList.setDisable(true);
            incomeList.setDate(TimeUtil.getCurrentTime());
            incomeMapper.updateByPrimaryKey(incomeList);
        }

        return "ok";
    }

    public String deleteIncome(String id) {
        int i = incomeMapper.deleteByPrimaryKey(id);
        return "ok";
    }


    public List<IncomeChart> topleftmethod(String queryTime, String queryname, User user) {
        Set<String> names = new HashSet<String>();
        if (queryname.isEmpty()) {
            queryname = "family";
        }
        if (queryname.equals("family")) {
            //先找出家庭人员
            List<Account> accounts = accountMapper.getFamilyName(user.getName());

            for (Account account : accounts
            ) {
                names.add(account.getName());
                names.add(account.getAccount());
            }
        } else {
            names.add(queryname);
        }
        String queryTimeStart = TimeUtil.getfirstDay(queryTime);
        String queryTimeEnd = TimeUtil.getlastDay(queryTime);
        List<Income> incomeList = incomeMapper.getByName(names, queryTimeStart, queryTimeEnd, queryname);
        Map<String, Double> map = new HashMap<>();
        List<IncomeChart> optionalerList = new ArrayList<>();
        for (Income icome : incomeList) {
            double value = 0;
            if (map.keySet().contains(icome.getSource())) {
                value = map.get(icome.getSource()) + icome.getMoney();
                map.put(icome.getSource(), value);
            } else {
                value = icome.getMoney();
                map.put(icome.getSource(), value);
            }

        }
        map.forEach((key, value) -> {
            IncomeChart optionaler = new IncomeChart();
            optionaler.setValue(value);
            optionaler.setName(key);
            optionalerList.add(optionaler);
        });
        return optionalerList;
    }

    public List<String> downrightmethod(User user, String queryname, String queryTime) {
        Set<String> names = new HashSet<String>();
        if (queryname.isEmpty()) {
            queryname = "family";
        }
        if (queryname.equals("family")) {
            //先找出家庭人员
            List<Account> accounts = accountMapper.getFamilyName(user.getName());

            for (Account account : accounts
            ) {
                names.add(account.getName());
                names.add(account.getAccount());
            }
        } else {
            names.add(queryname);
        }
        List<Income> incomeList = incomeMapper.getByName2(names, "", "", queryname);
        String year = queryTime.substring(0,4);

        List<String> stringList = charutile.getcharYear(incomeList,year);

        return stringList;
    }
}
